package com.ctsi.security.event.organ;

import com.ctsi.commons.event.AbstractObserver;
import com.ctsi.framework.common.startrun.ComponentFactory;
import com.ctsi.security.manager.impl.DepartAdminFetchObjIds;
import com.ctsi.security.manager.impl.DishiAdminFetchObjIds;
import com.ctsi.security.manager.impl.OrganFetchObjIds;
import com.ctsi.security.manager.impl.SecurityImpl;

import java.util.List;
import java.util.Map;


@SuppressWarnings("unchecked")
public class UserLeaveOrganEvent extends AbstractObserver {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(UserLeaveOrganEvent.class);
	private static OrganEventService organEventService;
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7888088385977758221L;

	@Override
	public void onEvent(Object[] args) {
		logger.info("权限组织事件处理：组织结构人员离开组织");
		if (args.length > 1) {
			String userPartyId = (String) args[1];
			String organPartyId = (String) args[2];
			organEventService = this.getOrganEventService();
			organEventService.userLeaveOrganEvent(organPartyId, userPartyId);
			// 清除人员所对应的登录用户的缓存数据对象
			if (args.length > 3) {
				logger.info("清除该人员对应的登录用户的缓存！");
				List<Map> temp = (List<Map>) args[3];
				for (Map item : temp) {
					String loginId = (String) item.get("LOGINID");
					OrganFetchObjIds.removeCache(loginId);
					DepartAdminFetchObjIds.removeCache(loginId);
					DishiAdminFetchObjIds.removeCache(loginId);
					SecurityImpl.removeCacheUserRoles(loginId);
				}
			}

		} else {
			logger.warn("Event is ERROR");
		}

	}

	private OrganEventService getOrganEventService() {
		if (organEventService == null) {
			organEventService = (OrganEventService) ComponentFactory.getApplicationContext().getBean("organEventService");
		}
		return organEventService;
	}

}
